package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.*;
import com.ruoyi.domain.dto.SelectCourseDTO;
import com.ruoyi.service.ICourseService;
import com.ruoyi.service.IStudentCourseService;
import com.ruoyi.service.ITeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IStudentCourseService studentCourseService;
    /**
     * 查询【请填写功能名称】列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Course course) {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    @GetMapping("/getAllBySelect/{studentId}")
    public AjaxResult getAllBySelect(@PathVariable("studentId") Long studentId, @RequestParam(value = "courseName", required = false) String courseName, @RequestParam(value = "teachName", required = false) String teachName) {
        startPage();
        List<Course> courses = courseService.getAllBySelect(studentId, courseName, teachName);
        List<Course> newList = new ArrayList<>();
        for (int i = 0; i < courses.size(); i++) {
            Double avg = courseService.selectTeachAvg(courses.get(i).getTeacherName());
            courses.get(i).setAvg(avg);
            newList.add(i,courses.get(i));
        }
        TableDataInfo dataTable = getDataTable(newList);
        return AjaxResult.success(dataTable);
    }

    @GetMapping("/getRecommCourse/{studentId}")
    public AjaxResult getRecommCourse(@PathVariable("studentId") Long studentId, @RequestParam(value = "hobby", required = false) String hobby, @RequestParam(value = "studyExperience", required = false) String studyExperience, @RequestParam(value = "courseName", required = false) String courseName, @RequestParam(value = "teacherName", required = false) String teachName) {
        startPage();
        List<Course> courses = courseService.getRecommCourse(studentId, hobby, studyExperience,courseName,teachName);
        TableDataInfo dataTable = getDataTable(courses);
        return AjaxResult.success(dataTable);
    }

    @GetMapping("/getAdded")
    public AjaxResult getAdded(@RequestParam(value = "courseName", required = false) String courseName, @RequestParam(value = "teacherName", required = false) String teachName) {
        startPage();
        List<Course> courses = courseService.getAddedCourse(courseName, teachName);
        return AjaxResult.success(getDataTable(courses));
    }

    @GetMapping("/getUnAdded")
    public AjaxResult getUnAdded(@RequestParam(value = "courseName", required = false) String courseName, @RequestParam(value = "teachName", required = false) String teachName) {
        startPage();
        List<Course> courses = courseService.getUnAddedCourse(courseName, teachName);
        return AjaxResult.success(getDataTable(courses));
    }

    @GetMapping("/hobbyType")
    public AjaxResult getHobbyType() {
        List<CourseHobby> courses = courseService.getHobbyType();
        return AjaxResult.success(courses);
    }
    @GetMapping("/studyExperienceType")
    public AjaxResult getStudyExperienceType() {
        List<CourseStudyExperience> courses = courseService.getStudyExperienceType();
        return AjaxResult.success(courses);
    }

    @GetMapping("/getAllBySelected/{studentId}")
    public AjaxResult getAllBySelected(@PathVariable("studentId") Long studentId, @RequestParam(value = "courseName", required = false) String courseName, @RequestParam(value = "teachName", required = false) String teachName) {
        startPage();
        List<Course> courses = courseService.getAllBySelected(studentId, courseName, teachName);
        TableDataInfo dataTable = getDataTable(courses);
        return AjaxResult.success(dataTable);
    }

    @PostMapping("/unAdded/{courseId}")
    public AjaxResult unAdded(@PathVariable("courseId") Long courseId) {
        courseService.unAddedCourse(courseId);
        return AjaxResult.success();
    }

    @PostMapping("/added/{courseId}")
    public AjaxResult added(@PathVariable("courseId") Long courseId) {
        courseService.addedCourse(courseId);
        return AjaxResult.success();
    }

    @PostMapping("/saveAdded")
    public AjaxResult saveAdded(@RequestBody Course course) {
        course.setStatus("1");
        courseService.insertCourse(course);
        return AjaxResult.success();
    }

    @PostMapping("/saveUnAdded")
    public AjaxResult saveUnAdded(@RequestBody Course course) {
        course.setStatus("0");
        courseService.insertCourse(course);
        return AjaxResult.success();
    }

    @PostMapping("selectCourse")
    public AjaxResult selectCourse(@RequestBody SelectCourseDTO selectCourseDTO) {
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setCourseId(selectCourseDTO.getCourseId());
        studentCourse.setStudentId(selectCourseDTO.getStudentId());
        studentCourseService.insertStudentCourse(studentCourse);
        return AjaxResult.success();
    }

    @PostMapping("dropCourse")
    public AjaxResult dropCourse(@RequestBody SelectCourseDTO selectCourseDTO) {
        studentCourseService.dropCourse(selectCourseDTO);
        return AjaxResult.success();
    }

    /**
     * 导出【请填写功能名称】列表
     */

    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course) {

        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */

    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId) {
        return success(courseService.selectCourseByCourseId(courseId));
    }

    /**
     * 新增【请填写功能名称】
     */

    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course) {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改【请填写功能名称】
     */

    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course) {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除【请填写功能名称】
     */

    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds) {
        return toAjax(courseService.deleteCourseByCourseIds(courseIds));
    }
}
